<?php
namespace App\Utilitys;

use Illuminate\Http\Request;

class Upload
{
    /**
     * 上传
     */

    protected static $uploadSizeLimit = 50 * 1024 * 1023; //限制上传尺寸10M

    /**
     * 文件上传
     */
    public static function uploadFile($file)
    {
        $fileSize = $file->getClientSize(); //文件大小
        if(!$file) {
            return array('code'=>'-1', 'msg'=>'文件不存在');
        }
        if ($fileSize > self::$uploadSizeLimit) {
            return array('code'=>'-3', 'msg'=>'文件过大');
        }
        $extension = $file->getClientOriginalExtension(); //获取后缀
        $folderName = 'uploads/'.date('Ymd',time()).'/';
        $safeName = date('YmdHis',time()).rand(10,100).'.'.$extension;
        $file->move($folderName, $safeName);
        $filePath = $folderName.$safeName;
        $dataArr = array(
            'filePath' => $filePath,
            'fileName' => $file->getClientOriginalName(),
            'fileSize' => $file->getClientSize(),
            'fileExt' => $file->getClientOriginalExtension(),
        );
        return array('code'=>'0', 'msg'=>'上传成功', 'data'=>$dataArr);
    }
}